package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.work.WorkBO;
import com.ymk.bo.work.WorkToTypeBO;
import com.ymk.bo.work.WorkTypeBO;
import com.ymk.core.code.cmd.WorkToTypeCmdCode;
import com.ymk.core.code.resp.RespCode;
import com.ymk.service.IWorkService;
import com.ymk.service.IWorkToTypeService;
import com.ymk.service.IWorkTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Service
public class WorkToTypePostCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkToTypeBO reqData = super.getData(sign, WorkToTypeBO.class);
        List<Integer> idList = reqData.getWorkTypeIdList();
        if (Objects.nonNull(idList)) {
            for (Integer i : idList) {
                WorkToTypeBO workToTypeBO = new WorkToTypeBO();
                workToTypeBO.setWorkId(reqData.getWorkId());
                workToTypeBO.setWorkTypeId(i);
                this.workToTypeService.post(workToTypeBO);
            }

        } else {
            this.workToTypeService.post(reqData);
        }


        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        WorkToTypeBO reqData = super.getData(sign, WorkToTypeBO.class);
        List<Integer> idList = reqData.getWorkTypeIdList();

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", reqData.getWorkId())
                .setReturnStruct(sign.getReturnStruct());
        WorkBO workBO = this.workService.get(paramsBuilder.build());
        if (Objects.isNull(workBO)) {
            throw new MallException(RespCode.CODE_ERROR, "岗位不存在");
        }

        if (!Objects.isNull(reqData.getWorkTypeId())) {
            paramsBuilder.clearParam().addParam("id", reqData.getWorkTypeId());
            WorkTypeBO workType = workTypeService.get(paramsBuilder.build());
            if (Objects.isNull(workType)) {
                throw new MallException(RespCode.CODE_ERROR, reqData.getWorkTypeId() + "工种不存在");
            }
        }

        if (!Objects.isNull(idList)) {
            for (int i : idList) {
                paramsBuilder.clearParam().addParam("id", i);
                WorkTypeBO workTypeBO = workTypeService.get(paramsBuilder.build());
                if (!Objects.nonNull(workTypeBO)) {
                    throw new MallException(RespCode.CODE_ERROR, i + "工种不存在");
                }
            }
        }


        return null;
    }

    @Override
    public String getCmdCode() {
        return WorkToTypeCmdCode.WORK_TO_TYPE_POST;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Autowired
    private IWorkToTypeService workToTypeService;
    @Autowired
    private IWorkTypeService workTypeService;
    @Autowired
    private IWorkService workService;
}